package com.ljy.my_study.spring.tiny.study7.aop;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.aopalliance.intercept.MethodInterceptor;

public class JdkDynamicAopProxy implements AopProxy,InvocationHandler{

	private AdvisedSupport support;
	
	public JdkDynamicAopProxy(AdvisedSupport support) {
		this.support=support;
	}
	
	@Override
	public Object getProxy() {
		return Proxy.newProxyInstance(getClass().getClassLoader()
				, new Class[] {support.getSource().getTargetClass()}, this);
	}

	@Override
	public Object invoke(final Object proxy,final Method method,final Object[] args) throws Throwable {
		MethodInterceptor interceptor=support.getInterceptor();
		return interceptor.invoke(new ReflectiveMethodInvocation(support.getSource().getTarget(),method,args));
		//如果是将support.getSource().getTarget()改为proxy，则变成自己调用自己，出现死循环
//		return interceptor.invoke(new ReflectiveMethodInvocation(proxy,method,args));
	}
	
}
